package com.rongxue.controller.payment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rongxue.mapper.SMobileCodeMapper;
import com.rongxue.mapper.SUserMapper;
import com.rongxue.pojo.ObjectResult;
import com.rongxue.pojo.SUser;
import com.rongxue.pojo.XMLBean;
import com.rongxue.service.BankBusinessService;
import com.rongxue.service.CashInAndOutService;
import com.rongxue.service.Impl.CashInAndOutServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Controller
public class CashInAndOutController {

    @Autowired
    private CashInAndOutService cashInAndOutService;

    @Autowired
    private BankBusinessService bankBusinessService;

    @Autowired
    private HttpServletRequest request;
    @Autowired
    HttpServletResponse response;

    @Autowired
    private SMobileCodeMapper sMobileCodeMapper;

    @Autowired
    private SUserMapper sUserMapper;
    private static final Logger logger = LoggerFactory.getLogger(CashInAndOutController.class);


    /**
     * 定时任务
     */
    @Scheduled(cron = "0 30 0 * * ?")//每天零点30分执行
    //@Scheduled(cron="0/5 * *  * * ? ")
    public void taskInvestmentPayBack() {
        //清楚拦截的map
        CashInAndOutServiceImpl cashInAndOutService = new CashInAndOutServiceImpl();
        ConcurrentHashMap<String, Long> map = cashInAndOutService.map;
        map.clear();
        //清空短信
        sMobileCodeMapper.deleteMobileCode();
    }

    //充值
    @ResponseBody
    @RequestMapping(value = "/tender/recharge", method = RequestMethod.POST, produces = "application/json")
    public ObjectResult recharge(XMLBean xmlBean){
        if (xmlBean.getToken() == null || xmlBean.getToken().equals("") || xmlBean.getDevice_type() == null || xmlBean.getDevice_type().equals("")
                || xmlBean.getMoney() == null || xmlBean.getMoney().equals("")){
            return ObjectResult.build("0", "参数丢失,请重试");
        }
        ObjectResult objectResult = cashInAndOutService.recharge(xmlBean);

        return objectResult;
    }

    //用户提现
    @ResponseBody
    @RequestMapping(value = "/tender/withdraw_deposit", method = RequestMethod.POST, produces = "application/json")
    public ObjectResult withdrawDeposit(XMLBean xmlBean){

        if (xmlBean.getToken() == null || xmlBean.getToken().equals("") || xmlBean.getDevice_type() == null || xmlBean.getDevice_type().equals("")
                || xmlBean.getMoney() == null || xmlBean.getMoney().equals("")){
            return ObjectResult.build("0", "参数丢失,请重试");
        }
        ObjectResult objectResult = cashInAndOutService.withdrawDeposit(xmlBean);

        return objectResult;
    }

    //快捷充值回调
    @ResponseBody
    @RequestMapping(value = "/tender/rechargeNotify", method = RequestMethod.POST, produces = "application/json")
    public ObjectResult rechargeNotify(String TYPE, String VERSION, String RESPONSECODE, String RESPONSEMSG, String MCHNTCD, String MCHNTORDERID,
                                       String ORDERID, String AMT, String BANKCARD, String SIGN){
        logger.info("快捷充值回调报文=========="+"TYPE---"+TYPE+"VERSION---"+VERSION+"RESPONSECODE---"+RESPONSECODE+"RESPONSEMSG---"+RESPONSEMSG+"MCHNTCD---"+MCHNTCD
                +"MCHNTORDERID---"+MCHNTORDERID+"ORDERID---"+ORDERID+"AMT---"+AMT+"BANKCARD---"+BANKCARD+"SIGN---"+SIGN);
        ObjectResult ObjectResult = cashInAndOutService.verificationrechargeNotify(TYPE, VERSION, RESPONSECODE, RESPONSEMSG, MCHNTCD, MCHNTORDERID, ORDERID, AMT, BANKCARD, SIGN);
        return ObjectResult;
    }


    //认证支付充值回调
    @ResponseBody
    @RequestMapping(value = "/tender/bankRechargeNotify", method = RequestMethod.POST, produces = "application/json")
    public ObjectResult bankRechargeNotify(String fy_ssn, String mchnt_cd, String order_amt, String order_date, String order_id, String order_st,
                                       String order_pay_code,String order_pay_error, String resv1, String md5){
        logger.info("手机银行充值回调报文=========="+"fy_ssn---"+fy_ssn+"mchnt_cd---"+mchnt_cd+"order_amt---"+order_amt+"order_date---"+order_date+"order_id---"+order_id
                +"order_st---"+order_st+"order_pay_code---"+order_pay_code+"order_pay_errot---"+order_pay_error+"resv1---"+resv1+"md5---"+md5);
        ObjectResult ObjectResult = cashInAndOutService.bankRechargeNotify(fy_ssn,  mchnt_cd, order_amt, order_date,  order_id,  order_st, order_pay_code, order_pay_error,  resv1,  md5);
        return ObjectResult;
    }

    //绑卡,富友验证
    @ResponseBody
    @RequestMapping(value = "/tender/binding_bank_no", method = RequestMethod.POST, produces = "application/json")
    public ObjectResult bindingBankNo(XMLBean xmlBean){
        if (xmlBean.getBank_no() == null || xmlBean.getBank_no().equals("") || xmlBean.getCard_no() == null || xmlBean.getCard_no().equals("") || xmlBean.getReal_name() == null
                || xmlBean.getReal_name().equals("") || xmlBean.getMobile() == null || xmlBean.getMobile().equals("")){
            return ObjectResult.build("0", "参数丢失,请重试");
        }
        ObjectResult objectResult = cashInAndOutService.bindingBankNo(xmlBean);

        return objectResult;
    }

    //企业借款人绑卡
    @ResponseBody
    @RequestMapping(value = "/tender/enterpriseBindingBankNo", method = RequestMethod.POST, produces = "application/json")
    public ObjectResult enterpriseBindingBankNo(XMLBean xmlBean){
        if (xmlBean.getBank_no() == null || xmlBean.getBank_no().equals("") || xmlBean.getCard_no() == null || xmlBean.getCard_no().equals("") || xmlBean.getReal_name() == null
                || xmlBean.getReal_name().equals("") || xmlBean.getMobile() == null || xmlBean.getMobile().equals("") ||xmlBean.getBank_name()==null ||
                xmlBean.getBank_name().equals("") || xmlBean.getBank_code()==null || xmlBean.getBank_code().equals("")){
            return ObjectResult.build("0", "参数丢失,请重试");
        }
        ObjectResult objectResult = cashInAndOutService.enterpriseBindingBankNo(xmlBean);

        return objectResult;
    }
}

